import { Supply } from './../../../../@core/data/supplier';
import { Component, Input, ViewChild } from '@angular/core';
import { DataTableComponent, DialogService, TableWidthConfig } from 'ng-devui';
import { DataSharingService } from 'src/app/@core/services/dataSharing/data-sharing.service';
import { PurchaseService } from 'src/app/@core/services/purchase/purchase.service';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';
import { environment } from 'src/environments/environment';

@Component({
  selector: 'app-select-arrival',
  templateUrl: './select-arrival.component.html',
  styleUrls: ['./select-arrival.component.scss']
})
export class SelectArrivalComponent {
  @Input() data:any
  fifter = {
    PageSize: 20,
    PageNumber: 1,
    StartTime: "",
    EndTime: "",
    PurchaseArrivalOrderNum: "", // 需要添加的字段
    PurchaseReturnedOrderNum: "", // 需要添加的字段
    isClosed: false,
    Supply: "",
    Status: "审核通过",
    Part: "",
    ZhuanHuanWanDe: false,
  }
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  }
  pageSizeOptions = [10, 20, 30, 50, 100]
  tableData = []
  dataTableOptions = {
    columns: [
      {
        field: 'name',
        header: '采购到货单号',
        fieldType: 'text',
        fixedLeft: "80px",
      },
      {
        field: 'name',
        header: '单据日期',
        fieldType: 'text',
      },
      {
        field: '物料编号',
        header: '物料编号',
        fieldType: 'text'
      },
      {
        field: '图片',
        header: '图片',
        fieldType: 'text'
      },
      {
        field: '物料名称',
        header: '物料名称',
        fieldType: 'text'
      },
      {
        field: '规格型号',
        header: '规格型号',
        fieldType: 'text'
      },
      {
        field: '物料单位',
        header: '物料单位',
        fieldType: 'text'
      },
      {
        field: '入库数量',
        header: '入库数量',
        fieldType: 'text'
      },
      {
        field: '已退货数量',
        header: '已退货数量',
        fieldType: 'text'
      },
      {
        field: '未退货数量',
        header: '未退货数量',
        fieldType: 'text'
      },
      {
        field: '到货日期',
        header: '到货日期',
        fieldType: 'text'
      },
      {
        field: '业务员',
        header: '业务员',
        fieldType: 'text'
      },
      {
        field: '部门',
        header: '部门',
        fieldType: 'text'
      },
      {
        field: '供应商全称',
        header: '供应商全称',
        fieldType: 'text'
      },
      {
        field: '备注',
        header: '备注',
        fieldType: 'text'
      },
      {
        field: '制单人',
        header: '制单人',
        fieldType: 'text'
      },
      {
        field: '制单日期',
        header: '制单日期',
        fieldType: 'text'
      },
      {
        field: '供应商',
        header: '供应商',
        fieldType: 'text',
        fixedRight: "0px",
      }
    ]
  }//表格数据
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'checkbox',
      width: '80px'
    },
    {
      field: '采购订单号',
      width: '200px'
    },
    {
      field: '单据日期',
      width: '170px'
    },
    {
      field: '物料编号',
      width: '150px'
    },
    {
      field: '图片',
      width: '150px'
    },
    {
      field: '物料名称',
      width: '150px'
    },
    {
      field: '规格型号',
      width: '150px'
    },
    {
      field: '物料单位',
      width: '150px'
    },
    {
      field: '入库数量',
      width: '150px'
    },
    {
      field: '已退货数量',
      width: '150px'
    },
    {
      field: '为退货数量',
      width: '150px'
    },
    {
      field: '到货日期',
      width: '150px'
    },
    {
      field: '业务员',
      width: '150px'
    },
    {
      field: '部门',
      width: '150px'
    },
    {
      field: '供应商',
      width: '150px'
    },
    {
      field: '供应商全称',
      width: '150px'
    },
    {
      field: '备注',
      width: '150px'
    },
    {
      field: '制单人',
      width: '150px'
    },
    {
      field: '制单日期',
      width: '150px'
    },
  ]//表格宽度配置
  msgs: { severity: any; summary: any; content: any; }[]
  @ViewChild(DataTableComponent, { static: true }) datatable: DataTableComponent;
  checkedIds: any = []
  date=[]
  status = [
    "待审核",
    "审核通过",
    "审核不通过",
  ]
  filterRows: any[]=[]
  constructor(private SharedMethodService: SharedMethodService, private PurchaseService: PurchaseService,) { }
  ngOnInit(): void {
    this.filter()
  }
  filter() {
    this.fifter.Supply = this.data.Supply
    this.PurchaseService.GetPagedPurchaseArrivalOrderParts(this.fifter).subscribe((data: any) => {
      const filterIds = this.data.purchaseOrderPartIds;
      // 首先使用filter方法过滤掉不符合条件的项
      const filteredItems = data.items.filter((item: any) => item.arrivalQuantity - item.returnedQuantity > 0);
      // 然后对过滤后的项进行map操作
      this.tableData = data.items.map((item: any) => {
        if (filterIds.includes(item.purchaseArrivalOrderPartId)) {
          return {
            ...item,
            $checked: true,
            $halfChecked: false
          };
        }
        return item;
      });
      this.pager.total = data.totalCount;
      this.CheckedRows = this.tableData.filter((item: any) => item.$checked);
    });
  }
  filter2() {
    this.fifter.PageNumber = 1;
    this.filter();
  }
  reset() {
    this.fifter = {
      PageSize: 20,
      PageNumber: 1,
      StartTime: "",
      EndTime: "",
      PurchaseArrivalOrderNum: "",
      PurchaseReturnedOrderNum: "",
      isClosed: false,
      Supply: "",
      Status: "审核通过",
      Part: "",
      ZhuanHuanWanDe: false,
    }
    this.date = []
    this.filter()
  }
  onRowCheckChange(checked: any, rowIndex: any, nestedIndex: any, rowItem: { $checked: any; $halfChecked: boolean; }) {
    this.SharedMethodService.onRowCheckChange(checked, rowIndex, nestedIndex, rowItem, this.datatable)
  }
  getCheckedRows() {
    this.SharedMethodService.getCheckedRows(this.datatable, 'purchaseRequiredOrderId').subscribe((data: any) => {
      this.checkedIds = data;
    })
  }//获取选中行
  CheckedRows = []
  getCheckedRowsData() {
    this.SharedMethodService.getCheckedRowsData(this.datatable).subscribe((data: any) => {
      this.CheckedRows = data;
      const filterIds = this.data.purchaseOrderPartIds;
      this.tableData = this.tableData.map((item: any) => {
        if (filterIds.includes(item.purchaseArrivalOrderPartId)) {
          return {
            ...item,
            $checked: true,
            $halfChecked: false,
          };
        }
        return item;
      });
      this.filterRows = this.CheckedRows.filter(
        (item: any) => !filterIds.includes(item.purchaseArrivalOrderPartId)
      );
    })
  }//获取选中行数据
  cleared() {
    this.reset();
  }//清空选中行
  pageIndexChange($event: any) {
    this.filter();
  }//切换页码
  pageSizeChange($event: any) {
    this.filter();
  }//改变每页显示数量
  showToast(type: any, summary: any, content: any) {
    switch (type) {
      default:
        this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  }//提示框
  getImagesArray(fileURL: string): string[] {
    let urls = fileURL.split(',');
    urls = urls.map(url => environment.modifiedApiUrl + url);
    return urls;
  }
  dateChange($event:any){
    this.fifter.StartTime = this.date[0].toISOString().split('T')[0]
    this.fifter.EndTime = this.date[1].toISOString().split('T')[0]
  }
}
